package com.gtq.uhome.service.serviceImpl;

import com.gtq.uhome.mapper.LogisticsMapper;
import com.gtq.uhome.service.LogisticsService;
import com.gtq.uhome.vo.LogisticsVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * 站点后台，派送列表
 */
@Service("logisticsService")
public class LogisticsServiceImpl implements LogisticsService {

    @Resource
    LogisticsMapper logisticsMapper = null;

    //新建物流单
    @Override
    @Transactional
    public Boolean insertLogistics(LogisticsVo logisticsVo) {

        Integer lines10 = -1;

        //设置状态为0表示存在
        logisticsVo.setStatusCode("0");
        //对user表添加数据,设置默认密码
        logisticsVo.setUserPassword("123456");
        //设置订单编号
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        logisticsVo.setOrderId(sdf.format(new Date()));

        //获取logisticsId
        if(logisticsVo.getAgingCode().equals("生鲜")){
            logisticsVo.setLogisticsId(logisticsVo.getOrderId()+"-1");
        }
        if(logisticsVo.getAgingCode().equals("常温")){
            logisticsVo.setLogisticsId(logisticsVo.getOrderId()+"-2");
        }

        Integer lines1 = logisticsMapper.insertUser(logisticsVo);
        List<LogisticsVo> userId = logisticsMapper.findUserIdInUser(logisticsVo);
        //获取userId
        for (LogisticsVo vo : userId) {
            logisticsVo.setUserId(vo.getUserId());
        }

        if(lines1 == 1){
            //对consignee表添加数据
            Integer lines2 = logisticsMapper.insertConsignee(logisticsVo);

            if(lines2 == 1){
                //获取consigneeId
                List<LogisticsVo> consigneeId = logisticsMapper.findConsigneeIdInConsignee(logisticsVo);
                for (LogisticsVo vo : consigneeId) {
                    logisticsVo.setConsigneeId(vo.getConsigneeId());
                }

                //设置提交时间
                logisticsVo.setSubTime(new Timestamp(Calendar.getInstance().getTimeInMillis()));
                //对order_detail表添加
                Integer lines3 = logisticsMapper.insertOrderDetail(logisticsVo);

                if(lines3 == 1){
                    //对order_sup_relation表添加
                    Integer lines4 = logisticsMapper.insertIntoOrderSupRelation(logisticsVo);
                    if(lines4 == 1){
                        //对consignee_order_relation表添加
                        Integer lines5 = logisticsMapper.insertConsigneeOrderRelation(logisticsVo);
                        if(lines5 == 1){
                            //对user_site表添加
                            //假数据
                            logisticsVo.setSiteId("1");
                            Integer lines6 = logisticsMapper.insertUserSite(logisticsVo);
                            if(lines6 == 1){
                                //对user_order_relation表添加
                                Integer lines7 = logisticsMapper.insertUserOrderRelation(logisticsVo);
                                if(lines7 == 1){
                                    //对order表添加
                                    logisticsVo.setCheckStatus("待审核");
                                    Integer lines8 = logisticsMapper.insertOrder(logisticsVo);
                                    if(lines8 == 1){
                                        //对order_site_relation表添加
                                        Integer lines9 = logisticsMapper.insertOrderSiteRelation(logisticsVo);
                                        if(lines9 == 1){
                                            //对delivery表进行添加
                                            System.out.println("进入最后一部了-----------------");
                                            System.out.println(logisticsVo.getConsigneeId()+"----------");
                                            System.out.println(logisticsVo);
                                            logisticsVo.setCashNumber("1");
                                            logisticsVo.setSerialNumber("000000022");
                                            lines10 = logisticsMapper.insertDelivery(logisticsVo);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }


        //判断是否添加成功
        return lines10 == 1;
    }

    //查询物料名称
    @Override
    public List<LogisticsVo> findSupName() {

        return logisticsMapper.findSupName();
    }

    //查询商品列表
    @Override
    public List<LogisticsVo> findCategoryName() {

        return logisticsMapper.findCategoryName();
    }
}
